#!/bin/bash

#if [ "$1" == "debug" ]
#then
#  set -x
#fi

scripts=../..
checkout=slave

mkdir script_checkout
cd script_checkout || exit 1
if svn co http://ootermite.googlecode.com/svn/trunk/buildbot/slave 
then
  true
else
  echo remove script_checkout and retry \"svn co\"
  rm -rf ../script_checkout/* || exit 1
  svn co http://ootermite.googlecode.com/svn/trunk/buildbot/slave || exit 1
fi
cd $checkout || exit 1

warn=0
rm $scripts/modified_scripts
touch $scripts/modified_scripts

for file in cws_fetch svn-clean configure buildprep bootstrap ccsetup.sh compile Xvfb_stop.sh getOOoEnv smoketestoo bundle steps.config.sample subsequenttests bot_backup
do
  uifile=\"$file\"
  err=
  if [ -f ../$scripts/$file ]
  then
    mv $file $file.co
    cp ../$scripts/$file $file
    if [ "`svn diff $file`" ]
    then
      ok=
      echo
      echo File $uifile not up to date. Trying update.
      for rev in `svn log $file |grep "^r[0-9]* | "| head -n 5 | sed -e "s/r//" -e "s/ .*//"`
      do
        if [ -z "$ok" ]
        then
          echo checking against revision $rev of $uifile
          if [ -z "`svn diff -r $rev $file || echo in case of failure of svn command have something to prevent unwanted copying of file`" ]
          then
            echo "======>> revision matches, copying new file $uifile"
            cp ../$scripts/$file ../$scripts/$file.bak || err="err"
            [ "$err" ] || cp $file.co ../$scripts/$file || err="err"
            ok=ok
          fi
        fi
      done
      if [ -z "$ok" ]
      then
        echo warning: could not update file $uifile. Seems to be modified locally
        echo ../$file >> $scripts/modified_scripts
        if [ "$1" == "debug" ]
        then
          diff -u $file.co ../$scripts/$file
        fi
      fi
    fi
  else
    echo
    echo "======>> file $uifile does not exist. installing"
    cp $file ../$scripts/$file || err="err"
  fi
  if [ "$err" ]
  then
    echo warning: could not update file $uifile. Problem accessing file
    warn=2
  fi

done

#copy new worker. will be renamed by maintenance script
if [ "`diff maintenance_worker ../$scripts/maintenance_worker`" ] ; then
  cp maintenance_worker ../$scripts/maintenance_worker_new
fi

cd ../.. || exit 1

rm -rf script_checkout

#touch flag-file that office is not running and remainders can be removed
touch kill_office

#clean up tempdirs by removing all files older than 30 days
if [ "$USER" ]
then
  echo cleaning /tmp
  find /tmp  -user $USER -type f -mtime +30 -exec rm -f {} \;
  if [ "$TMP" ]
  then
    echo cleaning $TMP
    find "$TMP"  -user $USER -type f -mtime +30 -exec rm -f {} \;
  fi
  if [ "$TEMP" ]
  then
    echo cleaning $TEMP
    find "$TEMP" -user $USER -type f -mtime +30 -exec rm -f {} \;
  fi
  if [ "$TMPDIR" ]
  then
    echo cleaning $TMPDIR
    find "$TMPDIR"  -user $USER -type f -mtime +30 -exec rm -f {} \;
  fi
else
  echo 'warning: $USER not set -> no automatic cleanup of temp directories'
fi

#call backup bundeling now
#this creates a tar archive in $HOME containing all relevant files
../bot_backup || warn=$?

echo $warn
exit $warn

# other files in cvs but not updated
bundle.soli
bundle.sols
bundle.win
cws_fetch.cfg
maintenance
maintenance_worker
README
sloccount
